package com.ruoyi.project.stop.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.mqtts.MqttClientConnect;
import com.ruoyi.common.redis.RedisCache;
import com.ruoyi.common.redis.RedisConst;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.stop.domain.ShipRecord;
import com.ruoyi.project.stop.domain.StopRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.stop.mapper.StopRecordMapper;
import com.ruoyi.project.stop.domain.StopRecord;
import com.ruoyi.project.stop.service.IStopRecordService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 一键急停记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
@Slf4j
public class StopRecordServiceImpl implements IStopRecordService
{
    @Autowired
    private StopRecordMapper stopRecordMapper;

    @Autowired
    private ShipRecordServiceImpl shipRecordServiceImpl;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询一键急停记录
     *
     * @param id 一键急停记录主键
     * @return 一键急停记录
     */
    @Override
    public StopRecord selectStopRecordById(Long id)
    {
        return stopRecordMapper.selectStopRecordById(id);
    }

    @Override
    public StopRecord selectStopRecordByIdLast() {
//        StopRecord stopRecord = new StopRecord();
//        String redisStr =  JSON.toJSONString(redisCache.getCacheObject(RedisConst.STOP_LAST_INFO));
//        // 查询redis中的最新数据
//        if(ObjectUtil.isNotEmpty(redisStr) &&  !"null".equals(redisStr)){
//            stopRecord = JSON.parseObject(redisStr, StopRecord.class);
//        }else {
//            stopRecord = stopRecordMapper.selectStopRecordByIdLast();
//            redisCache.setCacheObject(RedisConst.STOP_LAST_INFO, stopRecord, 1, TimeUnit.HOURS);
//        }
        return stopRecordMapper.selectStopRecordByIdLast();
    }

    /**
     * 查询一键急停记录列表
     *
     * @param stopRecord 一键急停记录
     * @return 一键急停记录
     */
    @Override
    public List<StopRecord> selectStopRecordList(StopRecord stopRecord)
    {
        return stopRecordMapper.selectStopRecordList(stopRecord);
    }

    /**
     * 新增一键急停记录
     *
     * @param StopRecordVO 一键急停记录
     * @return 结果
     */
    @Override
    public int insertStopRecord(StopRecordVO stopRecordVO)
    {
        log.info("stopRecordVO ：" + JSONObject.toJSONString(stopRecordVO));
        StopRecord stopRecord = new StopRecord();
        BeanUtils.copyProperties(stopRecordVO, stopRecord);
        try {
            ConcurrentHashMap<String, MqttClientConnect> mqttClients = MqttClientConnect.mqttClients;
            MqttClientConnect mqttClientConnect = mqttClients.get("1");
            //发送mqtt
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            JSONObject stopJson = new JSONObject();
            stopJson.put("stopType", stopRecordVO.getStopType());
            stopJson.put("stopStatus", stopRecordVO.getStopStatus());
            stopJson.put("cdate", simpleDateFormat.format(new Date()));
            mqttClientConnect.pub("stopRecord", stopJson.toJSONString());
            //如果是解除报警，需要发送mqtt
            if(stopRecordVO.getRemoveType() == 5L){
                log.info("发送 topic removeRecord " + JSONObject.toJSONString(stopRecordVO));
                //mqttClientConnect.pub("removeRecord", "removeRecord");
            }
        }catch (Exception e){
            log.error("云卓发送停止输入信号失败");
            e.printStackTrace();
        }
        stopRecord.setCreateTime(DateUtils.getNowDate());
        ShipRecord shipRecord = shipRecordServiceImpl.selectShipRecordByIdLast();
        stopRecord.setShipId(shipRecord.getId());

        int count = stopRecordMapper.insertStopRecord(stopRecord);
        if(1 == count){
            //覆盖redis
            redisCache.setCacheObject(RedisConst.STOP_LAST_INFO, stopRecord, 1, TimeUnit.HOURS);
        }
        return count;
    }

    /**
     * 修改一键急停记录
     *
     * @param stopRecord 一键急停记录
     * @return 结果
     */
    @Override
    public int updateStopRecord(StopRecord stopRecord)
    {
        stopRecord.setUpdateTime(DateUtils.getNowDate());
        return stopRecordMapper.updateStopRecord(stopRecord);
    }

    /**
     * 批量删除一键急停记录
     *
     * @param ids 需要删除的一键急停记录主键
     * @return 结果
     */
    @Override
    public int deleteStopRecordByIds(String ids)
    {
        return stopRecordMapper.deleteStopRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除一键急停记录信息
     *
     * @param id 一键急停记录主键
     * @return 结果
     */
    @Override
    public int deleteStopRecordById(Long id)
    {
        return stopRecordMapper.deleteStopRecordById(id);
    }
}
